package com.atguigu.demo03;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author gjl
 * @create 2021-04-26 11:20
 * //死锁例子
 */
public class Demo {
    public static void main(String[] args) {

        final Object objectLockA = new Object();
        final Object objectLockB = new Object();

        new Thread(()->{
            synchronized (objectLockA){
                System.out.println(Thread.currentThread().getName()+"有锁 想要B锁");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (objectLockB){
                    System.out.println(Thread.currentThread().getName()+"成功获得B锁");
                }
            }
        },"A").start();

        new Thread(()->{
            synchronized (objectLockB){
                System.out.println(Thread.currentThread().getName()+"有锁 想要A锁");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (objectLockA){
                    System.out.println(Thread.currentThread().getName()+"成功获得A锁");
                }
            }
        },"B").start();
    }
}

